package com.cuhk.gulimall.ware.service.impl;

import com.cuhk.common.utils.R;
import com.cuhk.gulimall.ware.feign.ProductFeignService;
import com.cuhk.common.to.SkuStockTo;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuhk.common.utils.PageUtils;
import com.cuhk.common.utils.Query;

import com.cuhk.gulimall.ware.dao.WareSkuDao;
import com.cuhk.gulimall.ware.entity.WareSkuEntity;
import com.cuhk.gulimall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId)){
            wrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void updateStock(Long skuId, Long wareId, Integer skuNum) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId);
        List<WareSkuEntity> entities = wareSkuDao.selectList(wrapper);
        // 如果当前wms_ware_sku中存在对应skuId和wareId的记录
        // 就更新
        if(entities != null && !entities.isEmpty()) {
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }
        // 不存在就是新增
        else {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            // 如果调用远程服务失败
            // 整个事务不需要回滚
            // 自己编写catch异常的逻辑
            // 不要抛出
            try {
                R info = productFeignService.info(skuId);
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                if(info.get("code").equals(HttpStatus.SC_OK)) {
                    wareSkuEntity.setSkuName(data.get("skuName").toString());
                }
            } catch (Exception e){
                e.printStackTrace();
            }
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuDao.insert(wareSkuEntity);
        }
    }

    @Override
    public SkuStockTo getSkuStockVo(Long skuId) {
        Long stock = wareSkuDao.getSkuStock(skuId);
        if(stock != null) {
            SkuStockTo vo = new SkuStockTo();
            vo.setValidStock(stock > 0);
            vo.setStock(stock);
            vo.setSkuId(skuId);
            return vo;
        } else {
            return null;
        }
    }

}